import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Random;

import javax.swing.JComponent;
import javax.swing.JFrame;


public class PaintClient extends JComponent implements Runnable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	
	public static void main(String[] args) {
		
		
		JFrame frame=new JFrame("Network Paint");
	
		frame.setSize(800,600);
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		
		Applet applet=new Applet();
		applet.setSize(800,600);
		
		final PaintClient paintClient=new PaintClient(new Dimension(800,600));
		
		//applet.add(paintClient);
		//applet.setBackground(Color.BLUE);
		frame.add(paintClient);
		paintClient.init();
		frame.addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				paintClient.cs.state=ConnectionState.Disconnected;
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		});

	}
	
	
	
	Player player;
	boolean running;
	Image img;
	ClientSocket cs;
	
	
	
	class ClientSocket extends Thread{
		
		Socket socket;
		String hostName="localhost";
		int port=1234;
		ConnectionState state;
		
		Player playerData;
		
		public ClientSocket(Player player) {
			playerData=player;
		}
		
		public void run(){
			
			try {
				socket=new Socket(hostName,port);
				System.out.println("Connected..");
				state=ConnectionState.Connected;
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			DataOutputStream os;
			
			try {
				os=new DataOutputStream(socket.getOutputStream());
				
				while(true){
					if(state==ConnectionState.Connected){
						os.writeInt(playerData.x);
						os.writeInt(playerData.y);
					}else if(state==ConnectionState.Disconnected){
						os.writeInt(-1);
					}
					
					
				}
			} catch (IOException e) {
				System.out.println("Connection killed.");
				System.exit(0);
			}
			
			
		}
		
	}
	
	public PaintClient(Dimension size){
		
		player=new Player(0,0,Color.RED);
		
		setSize(size);
		Random r =new Random();
		player.x=r.nextInt(getWidth());
		player.y=r.nextInt(getHeight());
		cs=new ClientSocket(player);
		
	}
	
	public void init(){
		Thread thread=new Thread(this);
		thread.start();
		cs.start();
	}

	@Override
	public void run() {
		
		running=true;
		
		//Graphics g=getGraphics();
		while(running){
			
			
			repaint();
			
			try {
				Thread.sleep(6);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}

	
	
	@Override
	public void paintComponent(Graphics g) {
		
	super.paintComponent(g);
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(player.color);
		g.fillOval(player.x, player.y, 50, 50);
		g.drawString(String.valueOf(System.nanoTime()), 20, 20);
		
	}

	

}
